//定义操作面板的位置全局变量
var fLeft;
var fTop;
var fPosi;
//定义属性面板的位置全局变量
var pLeft;
var pTop;
var pPosi;
//定义图层面板的位置全局变量
var lLeft;
var lTop;
var lPosi;

//定义属性框 图层框 操作面板框 测量结果 tooltip框
var indexProp;
var indexLayer
var indexFunc
var indexRes
var indexTipEdit

//选择数据源弹出框的index
var selDataIndex;

//定义是否触发自适应高度方法的变量
var autoH = false;
//定义最近一次操作的页面数据对象
var lastFuncObj = {};
var lastPropObj = {};

//定义弹出tip的类型
var TipEditType;
var cEditType = '';

//所有操作面板弹出框方法
function fnFuncOpen(title, url, width, height, fnObj) {

    //判断拾取是否有效
    // if (SupportCheck(title) && !sgworld._Viewer.scene.pickPositionSupported) {
    //     layer.msg('功能不可用');
    //     return;
    // }

    autoH = false;
    var left;
    var anim;
    var cls;
    //判断按钮的位置
    fLeft = parseInt(fLeft);
    fTop = parseInt(fTop);
    if (fPosi == 'left') {
        left = fLeft + 65;
        cls = 'fadeInLeft'
    } else {
        left = fLeft - width - 30;
        cls = 'fadeInRight'
    }

    var $title = $('#layui-layer' + indexFunc).find('.layui-layer-title');
    title = title ? title : (lastFuncObj.title ? lastFuncObj.title : '点');
    if (indexFunc) {
        layer.close(indexFunc);
    }

    //获取相关回调事件
    var cancelFn = null;
    var successFn = null;
    var endFn = null;
    for (key in fnObj) {
        cancelFn = (key == 'cancel') ? fnObj[key] : cancelFn;
        successFn = (key == 'success') ? fnObj[key] : successFn;
        endFn = (key == 'end') ? fnObj[key] : endFn;
    }
    //iframe层-父子操作
    //默认点开显示标绘点
    if (!url && $.isEmptyObject(lastFuncObj)) {
        url = 'components/Plotting/point';
    } else if (!url && !$.isEmptyObject(lastFuncObj)) {
        url = lastFuncObj.url;
        title = lastFuncObj.title;
        width = lastFuncObj.width;
        height = lastFuncObj.height;
        fnObj = lastFuncObj.fnObj;
    }
    indexFunc = layer.open({
        skin: 'other-class ' + cls,
        type: 2,
        title: title,
        area: [width + 'px', height + 'px'],
        fixed: false, //不固定
        shade: false,
        content: url,
        resize: false,
        offset: [fTop, left],
        anim: -1,
        min: function (obj) {
            obj.hide();
            return false;
        },
        success: function (index,layero) {
            if (successFn && typeof successFn == 'function') {
                successFn(layero,index)
            }
            lastFuncObj = {
                "url": url,
                "title": title,
                "width": width,
                "height": height,
                "fnObj": fnObj
            };
        },
        cancel: function (index, layero) {
            if (cancelFn && typeof cancelFn == 'function') {
                cancelFn()
            }
        },
        end: function () {
            if (endFn && typeof endFn == 'function') {
                endFn()
            }
        }
    });

    // }
}

//弹出结果面板方法
function fnResOpen(title, url, width, height, fnObj) {
    // 获取相关回调事件
    var cancelFn = null;
    var successFn = null;
    var endFn = null;
    for (key in fnObj) {
        cancelFn = (key == 'cancel') ? fnObj[key] : cancelFn;
        successFn = (key == 'success') ? fnObj[key] : successFn;
        endFn = (key == 'end') ? fnObj[key] : endFn;
    }

    if (indexRes) {
        layer.close(indexRes);
    }
    //iframe层-父子操作
    var winW = $(window).width();
    indexRes = layer.open({
        skin: 'other-class fadeInRight',
        type: 2,
        title: title,
        area: [width + 'px', height + 'px'],
        fixed: false, //不固定
        shadeClose: false,
        resize: false,
        content: url,
        offset: ['100', winW - width - 75],
        anim: 5,
        min: function (obj) {
            obj.hide();
            return false;
        },
        success: function (obj) {
            if (successFn && typeof successFn == 'function') {
                successFn()
            }
        },
        cancel: function (index, layero) {
            if (cancelFn && typeof cancelFn == 'function') {
                cancelFn()
            }
        },
        end: function () {
            if (endFn && typeof endFn == 'function') {
                endFn()
            }
        }
    });
}


//图层面板弹出框方法
function fnLayerOpen(title, url, width, height) {
    var left;
    var anim;
    var cls;
    console.log(width)
    lLeft = parseInt(lLeft);
    lTop = parseInt(lTop);

    if (lPosi == 'left') {
        left = lLeft + 65;
        cls = 'fadeInLeft'
    } else {
        left = lLeft - width - 30;
        cls = 'fadeInRight'
    }
    if (!document.getElementById('slider') && parent.document.getElementById('slider')) {
        left = parent.window.innerWidth + lLeft - width - 65;
        cls = 'fadeInRight';
    }

    var $title = $('#layui-layer' + indexLayer).find('.layui-layer-title');
    if ($title.length > 0 && $title.text() == '图层') {
        $('#layui-layer' + indexLayer).css({
            'top': lTop,
            'left': left,
        });
        $('#layui-layer' + indexLayer).show();
    } else {
        if (indexLayer) {
            layer.close(indexLayer);
        }
        //iframe层-父子操作
        indexLayer = layer.open({
            skin: 'other-class ' + cls,
            type: 2,
            title: '图层',
            area: ['350px', '500px'],
            fixed: false, //不固定
            shade: false,
            resize: false,
            content: url,
            offset: [lTop, left],
            anim: -1,
            cancel: function (index, obj) {
                //判断是否存在右键菜单等
                var $obj = obj.find('iframe').contents();
                $obj.find('#newAndUpdateInput').length > 0 ? $obj.find('#newAndUpdateInput').hide() : '';
                $obj.find('#rMenu').length > 0 ? $obj.find('#rMenu').hide() : '';
                obj.hide();
                return false;
            },
            success: function (obj) {
            }
        });
    }
}

//所有属性弹出框方法
function fnPropsOpen(title, url, width, height, type, fnObj) {
    autoH = false;
    var left;
    var cls;
    //判断按钮的位置
    pLeft = parseInt(pLeft);
    pTop = parseInt(pTop);
    if (pPosi == 'left') {
        left = pLeft + 65
        cls = 'fadeInLeft'
    } else {
        left = pLeft - width - 30
        cls = 'fadeInRight'
    }
    var $title = $('#layui-layer' + indexProp).find('.layui-layer-title');
    var isTitle = title ? true : false;
    title = title ? title : (lastPropObj.title ? lastPropObj.title : '');

    //获取相关回调事件
    var cancelFn = null;
    var successFn = null;
    var endFn = null;
    for (key in fnObj) {
        cancelFn = (key == 'cancel') ? fnObj[key] : cancelFn;
        successFn = (key == 'success') ? fnObj[key] : successFn;
        endFn = (key == 'end') ? fnObj[key] : endFn;
    }

    if (indexProp) {
        layer.close(indexProp);
    }
    //iframe层-父子操作
    if (!url && $.isEmptyObject(lastPropObj)) return;

    else if (!url && !$.isEmptyObject(lastPropObj)) {
        url = lastPropObj.url;
        title = lastPropObj.title;
        width = lastPropObj.width;
        height = lastPropObj.height;
        type = lastPropObj.type;
        fnObj = lastPropObj.fnObj
    }
    indexProp = layer.open({
        skin: 'other-class ' + cls,
        type: type,
        title: title,
        area: [width + 'px', height + 'px'],
        fixed: false, //不固定
        shadeClose: false,
        resize: false,
        content: url1,
        offset: [pTop, left],
        anim: -1,
        min: function (obj) {
            obj.hide();
            return false;
        },
        success: function (obj) {
            lastPropObj = {
                "url": url,
                "title": title,
                "width": width,
                "height": height,
                "type": type,
                "fnObj": fnObj
            };

            scrollbarFn(obj, height, type);
            //执行传入参数的回调方法
            if (successFn && typeof successFn == 'function') {
                successFn();
            }
        },
        cancel: function (index, layero) {
            if (cancelFn && typeof cancelFn == 'function') {
                cancelFn();
            }
        },
        end: function () {
            if (endFn && typeof endFn == 'function') {
                endFn();
            }
        }
    });
}
